
package com.compuvest.web;

// <editor-fold defaultstate="collapsed" desc="import">
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
// </editor-fold>

public class CompuvestCookies implements java.io.Serializable {

// <editor-fold defaultstate="collapsed" desc="init">
    private static final long serialVersionUID = 1234567890L;

    public final static int MAX_AGE = 3888000; // 2592000 - 30 days; 3888000 - 45 days; 86400 - 1 day;  * 30 = 1 month
    public final static String COMPUVEST = "compuvest";

    private String advertSite;
    private String userName;
    private String advertValue;

    private String hostname;
    private String ipAddress;
// </editor-fold>

    public CompuvestCookies() {
    }

// <editor-fold defaultstate="collapsed" desc="setCompuvestCookies()">

    public void setCompuvestCookies(HttpServletRequest request, HttpServletResponse response) {

        hostname = request.getRemoteHost();
        ipAddress = request.getRemoteAddr();

        String cjaid = null, cjpid = null, cjsid = null, siteID = null;
        Cookie[] cookies = request.getCookies();
        if(cookies != null) {
            for (Cookie cookie : cookies) {
                switch (cookie.getName()) {
                    case "CJAID":
                        cjaid = cookie.getValue();
                        break;
                    case "CJPID":
                        cjpid = cookie.getValue();
                        break;
                    case "CJSID":
                        cjsid = cookie.getValue();
                        break;
                    case "siteID":
                        siteID = cookie.getValue();
                        break;
                }
            }
        }

//        Cookie cookie = new Cookie("JSESSIONID", request.getSession().getId());
//        cookie.setPath("/");
//        cookie.setMaxAge(MAX_AGE);
//        response.addCookie(cookie);

        // store in cookies advert site
        advertSite = getAdvertName(request.getParameter("advsite"));
        Cookie cookie = new Cookie("advert", advertSite);
        cookie.setPath("/");
        cookie.setMaxAge(MAX_AGE);
        response.addCookie(cookie);

        // store in cookies user name
        userName = getUserName(request.getParameter("user"));
        cookie = new Cookie("user", userName);
        cookie.setPath("/");
        cookie.setMaxAge(MAX_AGE);
        response.addCookie(cookie);

        // cj
        if("cj".equals(advertSite)) {
            if(cjaid == null) {
                cjaid = request.getParameter("CJAID");
                if(cjaid != null) {
                    cookie = new Cookie("CJAID", cjaid);
                    cookie.setPath("/");
                    cookie.setMaxAge(MAX_AGE);
                    response.addCookie(cookie);
                }
            }
            if(cjpid == null) {
                cjpid = request.getParameter("CJPID");
                if(cjpid != null) {
                   cookie = new Cookie("CJPID", cjpid);
                   cookie.setPath("/");
                   cookie.setMaxAge(MAX_AGE);
                   response.addCookie(cookie);
                }
            }
            if(cjsid == null) {
                cjsid = request.getParameter("CJSID");
                if(cjsid != null) {
                    cookie = new Cookie("CJSID", cjsid);
                    cookie.setPath("/");
                    cookie.setMaxAge(MAX_AGE);
                    response.addCookie(cookie);
                }
            }
            advertValue = (cjaid == null) ? "" : cjaid;
            advertValue += (cjpid == null) ? "" : '-' + cjpid;
            advertValue += (cjsid == null) ? "" : '-' + cjsid;
        }

        // links
        if("links".equals(advertSite)) {
            if(siteID == null) {
                siteID = request.getParameter("siteID");
                if(siteID != null) {
                    cookie = new Cookie("siteID", siteID);
                    cookie.setPath("/");
                    cookie.setMaxAge(MAX_AGE);
                    response.addCookie(cookie);
                }
            }
            advertValue = (siteID == null) ? "" : siteID;
        }

    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="getAdvertName()">

    private String getAdvertName(String site) {

        String name =  COMPUVEST;
        if(site != null) {
            name = site.replace("\'", "").replace("\"", "");
            name = name.length() > 15 ? name.substring(0,15) : name;
        }
        return name;
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="getUserName()">

    private String getUserName(String user) {

        String name =  "";
        if(user != null) {
            name = user.replace("\'", "").replace("\"", "");
            name = name.length() > 3 ? name.substring(0,3) : name;
        }
        return name;
    }
// </editor-fold>

    public String getAdvertSite()       { return advertSite == null ? COMPUVEST : advertSite; }
    public String getUserName()         { return userName == null ? "" : userName; }
    public String getAdvertValue()      { return advertValue == null ? "" : (advertValue.length() > 40 ? advertValue.substring(0,40) : advertValue); }
    public String getHostName()		{ return hostname == null ? "" : hostname; }
    public String getIPAddress()	{ return ipAddress == null ? "0" : ipAddress; }

}
